﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataAccessLibrary;

namespace BusinessLayer
{
   public class ClsDepartment
    {
       public string Department { get; set; }
        public int DepartmentID { get; set; }

        public DataTable GetAllDepartment()
        {
            using (OleDataClient data = new OleDataClient())
            {
                return data.GetDataTable("Select * from Department", "Department");
            }
        }

        public void GetDepartment(int departmentID)
        {
            using (OleDataClient data = new OleDataClient())
            {
                DataTable dt = data.GetDataTable("Select * from Department where DepartmentID=" + departmentID.ToString(), "Department");
                if (dt.Rows.Count > 0)
                {
                    DepartmentID = Convert.ToInt32(dt.Rows[0]["DepartmentID"]);
                    Department = dt.Rows[0]["Department"].ToString();
                }
            }
        }

        public bool SaveDepartment(string KeyForSave)
        {
            using (OleDataClient data = new OleDataClient())
            {
                if (KeyForSave == "add")
                {
                    string count = data.GetValue("select count(*) from Department where Department='" + Department + "'");
                    if (Convert.ToInt32(count) > 0)
                    {
                        throw new Exception("Designation already exist");
                    }
                    else
                    {
                        data.ExecuteNonQuery("Insert into Department (Department) values('" + Department + "')");
                    }

                }
                else
                {
                    string count = data.GetValue("select count(*) from Department where Department='" + Department + "' and DepartmentID NOT IN (" + DepartmentID.ToString() + ")");
                    if (Convert.ToInt32(count) > 0)
                    {
                        throw new Exception("Department already exist");
                    }
                    else
                    {
                        data.ExecuteNonQuery("Update Department set Department = '" + Department + "' where DepartmentID=" + DepartmentID.ToString());
                    }
                }
            }
            return true;
        }


        public void DeleteDepartment()
        {
            using (OleDataClient data = new OleDataClient())
            {
                data.ExecuteNonQuery("Delete from  Department where DepartmentID=" + DepartmentID.ToString());
            }
        }


    }
}
